OPTIONS NOFMTERR MPRINT PS=60 LS=80 sasautos =macro;
libname perm7 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/cardiac/2007';
libname perm '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/cardiac/2007/bkup';
libname res '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results';
libname hed '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/MA_files';
libname rg '/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/measures/tables/regression/rru';

run;

libname r '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru';


***PLAN CHARACTERISTICS;
data pcar;
 set rg.ppo_char;
 keep org_id plantypec;
proc sort;
 by org_id;

***HMO PLAN LEVEL RESULTS;
data hmo (drop= len);
 length var $ 9;
 set perm7.hmo_card_plan2;
 if org_id ~= "" and hmo_pats ~= .;
 len = length(varname);
 var = substr(varname,1,len-5);
 if substr(varname,len-1,1) = "f" then female = 1; else female = 0;
 if substr(varname,length(varname),1) = "c" then comorb = 1; else comorb = 0;
 if substr(varname,length(varname)-3,2) = "am" then ami = 1; else ami = 0;
 if substr(varname,length(varname)-3,2) = "an" then angina = 1; else angina = 0;
 if substr(varname,length(varname)-3,2) = "ca" then cad = 1; else cad = 0;
 if substr(varname,length(varname)-3,2) = "ch" then chf = 1; else chf = 0;

proc sort;
 by org_id;

data pcar;
 merge hmo (in=h) pcar (in=p);
 by org_id;
 if h;
 if p  then ppo = 1;
 else ppo = 0;

proc sql;
 create table hmo as select distinct * from hmo;

proc sort;
 by var org_id ami angina cad chf female comorb;


***FFS MATCHED PLAN LEVEL RESULTS;
***WE SPLIT BY MEASURE;
data evis (keep = org_id ami angina cad chf female comorb ed_vis_c1 rename = (ed_vis_c1=ffs_mean))
ivis (keep = org_id ami angina cad chf female comorb ip_vis_c1 rename = (ip_vis_c1=ffs_mean))
scost (keep = org_id ami angina cad chf female comorb surg_cost_c1 rename = (surg_cost_c1=ffs_mean))
icost (keep = org_id ami angina cad chf female comorb ip_cost_c1 rename = (ip_cost_c1=ffs_mean))
ecost (keep = org_id ami angina cad chf female comorb em_cost_c1 rename = (em_cost_c1=ffs_mean));
set perm7.plan_res2 (keep = id2 org_id grp 
ed_vis_c1 ip_vis_c1 surg_cost_c1
 ip_cost_c1 em_cost_c1);

if substr(grp,1,2) = "am" then ami = 1; else ami = 0;
if substr(grp,1,2) = "an" then angina = 1; else angina = 0;
if substr(grp,1,2) = "ca" then cad = 1; else cad = 0;
if substr(grp,1,2) = "ch" then chf = 1; else chf = 0;

if substr(grp,3,1) = "f" then female = 1; else female = 0;
if substr(grp,4,1) = "c" then comorb = 1; else comorb = 0;
 output evis;
 output ivis;
 output scost;
 output icost;
 output ecost; 

***FFS RESULTS ONE LINE PER PLAN/MEASURE;
data ffs;
 length var $ 9;
 set evis (in=a)
 ivis (in=b)
 scost (in=c)
 icost (in=d)
 ecost (in=e); 

if a then var = "ed_vis";
if b then var = "ip_vis";
if c then var = "surg_cost";
if d then var = "ip_cost";
if e then var = "em_cost";


proc sort;
 by var org_id ami angina cad chf female comorb;


***MERGE HMO & FFS PLAN LEVEL RESULTS;

data mg;
 merge hmo ffs;
 by var org_id ami angina cad chf female comorb;
 if hmo_mean ~= . and ffs_mean ~= .;
 if hmo_mean = 0 then hmo_zero = 1; else hmo_zero = 0; 
 if ffs_mean = 0 then ffs_zero = 1; else ffs_zero = 0;

bkup = ffs_mean;



if var in ("ip_vis", "ed_vis") then do
 hmo_mean = hmo_mean * 1000;
 ffs_mean = ffs_mean * 1000;
end;

if ami = 1 then type = 1;
 else if angina = 1 then type = 2;
 else if cad = 1 then type = 3;
 else if chf = 1 then type = 4;

proc sort;
 by org_id;


%macro m(var);
data mg;
 merge mg (in=m) ppo (keep=org_id in=p);
 by org_id;
 if m;
 if p then ppo = 1; else ppo = 0;
 if ppo = 0 then hmo = 1;
 all = 1;;
 if &var = 1;

proc sql;
 create table n_ov as select
 var, sum(hmo_pats) as n from mg
 group by var;

proc sql;
 create table n_cb as select
 var, comorb, sum(hmo_pats) as n from mg
 group by var, comorb;

proc sql;
 create table n_tp as select
 var, type, sum(hmo_pats) as n from mg
 group by var, type;

proc sql;
 create table n_all as select
 var, comorb, type, sum(hmo_pats) as n from mg
 group by var, comorb, type;

data n_ov; set n_ov;
 comorb = 99; type = 99;
data n_tp; set n_tp;
 comorb = 99; 
data n_cb; set n_cb;
 type = 99;

data n_edv (drop = var rename = (n=n_edv)) 
n_emc (drop = var rename = (n=n_emc)) 
n_ipc (drop = var rename = (n=n_ipc)) 
n_ipv (drop = var rename = (n=n_ipv)) 
n_sc (drop = var rename = (n=n_sc));
 set n_all n_tp n_cb n_ov;
 if var = "ed_vis" then output n_edv;
 if var = "em_cost" then output n_emc;
 if var = "ip_cost" then output n_ipc;
 if var = "ip_vis" then output n_ipv;
 if var = "surg_cost" then output n_sc;
proc sort data=n_edv; by comorb type;
proc sort data=n_emc; by comorb type;
proc sort data=n_ipc; by comorb type;
proc sort data=n_ipv; by comorb type;
proc sort data=n_sc; by comorb type;

data n;
 merge n_edv n_emc n_ipc n_ipv n_sc;
 by comorb type;


proc means noprint data=mg;
 var hmo_mean ffs_mean;
 class var;
 weight hmo_pats;
 output out=overall;
data overall (drop = _freq_ _type_ _stat_  _mean_);
 set overall;
  where _type_ = 1 and _stat_ = "MEAN";
  comorb = 99;
  type = 99;

proc means noprint data=mg;
 var hmo_mean ffs_mean;
 class var comorb;
 weight hmo_pats;
 output out=comorb;
data comorb (drop = _freq_ _type_ _stat_  _mean_);
 set comorb;
  where _type_ = 3 and _stat_ = "MEAN";
  type = 99;

proc means noprint data=mg;
 var hmo_mean ffs_mean;
 class var type;
 weight hmo_pats;
 output out=type;
data type (drop = _freq_ _type_ _stat_  _mean_);
 set type;
  where _type_ = 3 and _stat_ = "MEAN";
  comorb = 99;


proc means noprint data=mg;
 class var comorb type;
 var hmo_mean ffs_mean;
 weight hmo_pats;
 output out=temp;
run;


data temp (drop = _type_ _stat_ _freq_);
 set temp;
 where _stat_ = "MEAN" and _type_ = 7;

data temp;
 set comorb type overall temp;

data edv (rename = (hmo_mean = edv_hmo ffs_mean=edv_ffs))
emc (rename = (hmo_mean = emc_hmo ffs_mean=emc_ffs))
ipc (rename = (hmo_mean = ipc_hmo ffs_mean=ipc_ffs))
ipv (rename = (hmo_mean = ipv_hmo ffs_mean=ipv_ffs))
sc (rename = (hmo_mean = sc_hmo ffs_mean=sc_ffs));
 set temp;
 if var = "ed_vis" then output edv;
 if var = "em_cost" then output emc;
 if var = "ip_cost" then output ipc;
 if var = "ip_vis" then output ipv;
 if var = "surg_cost" then output sc;

proc sort data=edv;
 by comorb type;
proc sort data=emc;
 by comorb type;
proc sort data=ipc;
 by comorb type;
proc sort data=ipv;
 by comorb type;
proc sort data=sc;
 by comorb type;

data &var.fn  (drop=type comorb);
 length lab $ 30;
 merge n ipc emc sc edv ipv;
 by comorb type;
 drop var;

if type = 99 and comorb = 99 then do; sort = 1;
lab = "Overall"; end;

if type = 1 and comorb = 99 then do; sort = 2;
lab = "AMI"; end;

if type = 2 and comorb = 99 then do; sort = 3;
lab = "Angina"; end;

if type = 3 and comorb = 99 then do; sort = 4;
lab = "CAD"; end;

if type = 4 and comorb = 99 then do; sort = 5;
lab = "CHF"; end;

if type = 99 and comorb = 1 then do; sort = 6;
lab = "With Comorbidities"; end;

if type = 99 and comorb = 0 then do; sort = 7;
lab = "Without Comorbidities"; end;

if type = 1 and comorb = 1 then do; sort = 8;
lab = "AMI, With Comorbidities"; end;

if type = 1 and comorb = 0 then do; sort = 9;
lab = "AMI, Without Comorbidities"; end;

if type = 2 and comorb = 1 then do; sort = 10;
lab = "Angina, With Comorbidities"; end;

if type = 2 and comorb = 0 then do; sort = 11;
lab = "Angina, Without Comorbidities"; end;

if type = 3 and comorb = 1 then do; sort = 12;
lab = "CAD, With Comorbidities"; end;

if type = 3 and comorb = 0 then do; sort = 13;
lab = "CAD, Without Comorbidities"; end;

if type = 4 and comorb = 1 then do; sort = 14;
lab = "CHF, With Comorbidities"; end;

if type = 4 and comorb = 0 then do; sort = 15;
lab = "CHF, Without Comorbidities"; end;

emc_ratio = emc_hmo/emc_ffs;
sc_ratio = sc_hmo/sc_ffs;
edv_ratio = edv_hmo/edv_ffs;
ipv_ratio = ipv_hmo/ipv_ffs;
ipc_ratio = ipc_hmo/ipc_ffs;

if sort > 7 then delete;



proc sort;
 by sort;
run;
%mend m;

%m(ppo)
%m(hmo)
%m(all)



data ppofn;
 set ppofn;
 rename
 n_ipc = ppo_n_ipc 
 ipc_hmo = ppo_ipc_ma
 ipc_ffs = ppo_ipc_ffs
 n_emc = ppo_n_emc
 emc_hmo = ppo_emc_ma
 emc_ffs = ppo_emc_ffs
 n_sc = ppo_n_sc
 sc_hmo = ppo_sc_ma
 sc_ffs = ppo_sc_ffs
 n_edv = ppo_n_edv
 edv_hmo = ppo_edv_ma
 edv_ffs = ppo_edv_ffs
 n_ipv = ppo_n_ipv
 ipv_hmo = ppo_ipv_ma
 ipv_ffs = ppo_ipv_ffs;
proc sort;
 by lab;

data hmofn;
 set hmofn;
 rename
 n_ipc = hmo_n_ipc
 ipc_hmo = hmo_ipc_ma
 ipc_ffs = hmo_ipc_ffs
 n_emc = hmo_n_emc
 emc_hmo = hmo_emc_ma
 emc_ffs = hmo_emc_ffs
 n_sc = hmo_n_sc
 sc_hmo = hmo_sc_ma
 sc_ffs = hmo_sc_ffs
 n_edv = hmo_n_edv
 edv_hmo = hmo_edv_ma
 edv_ffs = hmo_edv_ffs
 n_ipv = hmo_n_ipv
 ipv_hmo = hmo_ipv_ma
 ipv_ffs = hmo_ipv_ffs;
proc sort;
 by lab;


data allfn;
 set allfn;
 rename
 n_ipc = all_n_ipc
 ipc_hmo = all_ipc_ma
 ipc_ffs = all_ipc_ffs
 n_emc = all_n_emc
 emc_hmo = all_emc_ma
 emc_ffs = all_emc_ffs
 n_sc = all_n_sc
 sc_hmo = all_sc_ma
 sc_ffs = all_sc_ffs
 n_edv = all_n_edv
 edv_hmo = all_edv_ma
 edv_ffs = all_edv_ffs
 n_ipv = all_n_ipv
 ipv_hmo = all_ipv_ma
 ipv_ffs = all_ipv_ffs;
proc sort;
 by lab;



/**************************************************************/
data all_edv (rename = (ffs_plan_se=all_edv_ffs_se hmo_plan_se=all_edv_ma_se))
all_ipv (rename = (ffs_plan_se=all_ipv_ffs_se hmo_plan_se=all_ipv_ma_se))
all_emc (rename = (ffs_plan_se=all_emc_ffs_se hmo_plan_se=all_emc_ma_se))
all_ipc (rename = (ffs_plan_se=all_ipc_ffs_se hmo_plan_se=all_ipc_ma_se))
all_sc (rename = (ffs_plan_se=all_sc_ffs_se hmo_plan_se=all_sc_ma_se)) 
ppo_edv (rename = (ffs_plan_se=ppo_edv_ffs_se hmo_plan_se=ppo_edv_ma_se))
ppo_ipv (rename = (ffs_plan_se=ppo_ipv_ffs_se hmo_plan_se=ppo_ipv_ma_se))
ppo_emc (rename = (ffs_plan_se=ppo_emc_ffs_se hmo_plan_se=ppo_emc_ma_se))
ppo_ipc (rename = (ffs_plan_se=ppo_ipc_ffs_se hmo_plan_se=ppo_ipc_ma_se))
ppo_sc (rename = (ffs_plan_se=ppo_sc_ffs_se hmo_plan_se=ppo_sc_ma_se))
hmo_edv (rename = (ffs_plan_se=hmo_edv_ffs_se hmo_plan_se=hmo_edv_ma_se))
hmo_ipv (rename = (ffs_plan_se=hmo_ipv_ffs_se hmo_plan_se=hmo_ipv_ma_se))
hmo_emc (rename = (ffs_plan_se=hmo_emc_ffs_se hmo_plan_se=hmo_emc_ma_se))
hmo_ipc (rename = (ffs_plan_se=hmo_ipc_ffs_se hmo_plan_se=hmo_ipc_ma_se))
hmo_sc (rename = (ffs_plan_se=hmo_sc_ffs_se hmo_plan_se=hmo_sc_ma_se));
 set perm7.cardiac_results_national (in=a)
 perm7.cardiac_results_ppo (in=b)
 perm7.cardiac_results_hmo (in=c);
 drop hmo_plan_mean ffs_plan_mean hmo_pats_sum;
 if tp = "NCM" then lab = "Without Comorbidities";
 if tp = "CMB" then lab = "With Comorbidities";
 if tp = "CAD" then lab = "CAD";
 if tp = "CHF" then lab = "CHF";
 if tp = "AMI" then lab = "AMI";
 if tp = "ANG" then lab = "Angina";

 if tp = "ALL" then lab = "Overall";


if a and var = "ed_vi" then output all_edv;
if a and var = "em_co" then output all_emc;
if a and var = "surg_" then output all_sc;
if a and var = "ip_co" then output all_ipc;
if a and var = "ip_vi" then output all_ipv;

if b and var = "ed_vi" then output ppo_edv;
if b and var = "em_co" then output ppo_emc;
if b and var = "surg_" then output ppo_sc;
if b and var = "ip_co" then output ppo_ipc;
if b and var = "ip_vi" then output ppo_ipv;

if c and var = "ed_vi" then output hmo_edv;
if c and var = "em_co" then output hmo_emc;
if c and var = "surg_" then output hmo_sc;
if c and var = "ip_co" then output hmo_ipc;
if c and var = "ip_vi" then output hmo_ipv;


proc sort data=all_edv; by lab;
proc sort data=all_emc; by lab;
proc sort data=all_sc; by lab;
proc sort data=all_ipc; by lab;
proc sort data=all_ipv; by lab;

proc sort data=ppo_edv; by lab;
proc sort data=ppo_emc; by lab;
proc sort data=ppo_sc; by lab;
proc sort data=ppo_ipc; by lab;
proc sort data=ppo_ipv; by lab;

proc sort data=hmo_edv; by lab;
proc sort data=hmo_emc; by lab;
proc sort data=hmo_sc; by lab;
proc sort data=hmo_ipc; by lab;
proc sort data=hmo_ipv; by lab;


data se;
 merge all_edv all_emc all_sc all_ipc all_ipv
 ppo_edv ppo_emc ppo_sc ppo_ipc ppo_ipv
 hmo_edv hmo_emc hmo_sc hmo_ipc hmo_ipv;
 by lab;
 drop tp org_id var;

/**************************************************************/



data mg;
 merge ppofn hmofn allfn se;
 by lab;
 ppo_ipc_ratio=ppo_ipc_ma/ppo_ipc_ffs;
 hmo_ipc_ratio=hmo_ipc_ma/hmo_ipc_ffs;
 all_ipc_ratio=all_ipc_ma/all_ipc_ffs;

 ppo_emc_ratio=ppo_emc_ma/ppo_emc_ffs;
 hmo_emc_ratio=hmo_emc_ma/hmo_emc_ffs;
 all_emc_ratio=all_emc_ma/all_emc_ffs;

 ppo_sc_ratio=ppo_sc_ma/ppo_sc_ffs;
 hmo_sc_ratio=hmo_sc_ma/hmo_sc_ffs;
 all_sc_ratio=all_sc_ma/all_sc_ffs;

 ppo_edv_ratio=ppo_edv_ma/ppo_edv_ffs;
 hmo_edv_ratio=hmo_edv_ma/hmo_edv_ffs;
 all_edv_ratio=all_edv_ma/all_edv_ffs;

 ppo_ipv_ratio=ppo_ipv_ma/ppo_ipv_ffs;
 hmo_ipv_ratio=hmo_ipv_ma/hmo_ipv_ffs;
 all_ipv_ratio=all_ipv_ma/all_ipv_ffs;
run;


proc sort;
 by sort;






ods html file = "rruipc2.xls";
proc print;
 var lab
ppo_n_ipc ppo_ipc_ma ppo_ipc_ffs ppo_ipc_ffs_se ppo_ipc_ma_se ppo_ipc_ratio
hmo_n_ipc hmo_ipc_ma hmo_ipc_ffs hmo_ipc_ffs_se hmo_ipc_ma_se hmo_ipc_ratio
all_n_ipc all_ipc_ma all_ipc_ffs all_ipc_ffs_se all_ipc_ma_se all_ipc_ratio
;
run;
ods html close;


ods html file = "rruemc2.xls";
proc print;
 var lab
ppo_n_emc ppo_emc_ma ppo_emc_ffs ppo_emc_ffs_se ppo_emc_ma_se ppo_emc_ratio
hmo_n_emc hmo_emc_ma hmo_emc_ffs hmo_emc_ffs_se hmo_emc_ma_se hmo_emc_ratio
all_n_emc all_emc_ma all_emc_ffs all_emc_ffs_se all_emc_ma_se all_emc_ratio
;
run;
ods html close;

ods html file = "rrusc2.xls";
proc print;
 var lab
ppo_n_sc ppo_sc_ma ppo_sc_ffs ppo_sc_ffs_se ppo_sc_ma_se ppo_sc_ratio
hmo_n_sc hmo_sc_ma hmo_sc_ffs hmo_sc_ffs_se hmo_sc_ma_se hmo_sc_ratio
all_n_sc all_sc_ma all_sc_ffs all_sc_ffs_se all_sc_ma_se all_sc_ratio
;
run;
ods html close;

ods html file = "rruedv2.xls";
proc print;
 var lab
ppo_n_edv ppo_edv_ma ppo_edv_ffs ppo_edv_ffs_se ppo_edv_ma_se ppo_edv_ratio
hmo_n_edv hmo_edv_ma hmo_edv_ffs hmo_edv_ffs_se hmo_edv_ma_se hmo_edv_ratio
all_n_edv all_edv_ma all_edv_ffs all_edv_ffs_se all_edv_ma_se all_edv_ratio
;
run;
ods html close;

ods html file = "rruipv2.xls";
proc print;
 var lab
ppo_n_ipv ppo_ipv_ma ppo_ipv_ffs ppo_ipv_ffs_se ppo_ipv_ma_se ppo_ipv_ratio
hmo_n_ipv hmo_ipv_ma hmo_ipv_ffs hmo_ipv_ffs_se hmo_ipv_ma_se hmo_ipv_ratio
all_n_ipv all_ipv_ma all_ipv_ffs all_ipv_ffs_se all_ipv_ma_se all_ipv_ratio
;
run;
ods html close;




